package com.woniu.his.config;

import com.woniu.his.dao.DocterInfoMapper;
import com.woniu.his.entity.DocterInfo;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;

import javax.print.Doc;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 自定义Realm
 */
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private DocterInfoMapper docterInfoMapper;

    /**
     *@描述  授权
     *@参数
     *@返回值
     *@创建人  li
     *@创建时间  2019/11/27 0027
     *@修改人和其它信息
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principalCollection) {
        SimpleAuthorizationInfo simpleAuthorizationInfo = new SimpleAuthorizationInfo();
        DocterInfo user = (DocterInfo) principalCollection.getPrimaryPrincipal();
        Integer id = user.getDutyId();
        if (id == 1) {
            simpleAuthorizationInfo.addRole("住院部");
        } else if (id == 2) {
            simpleAuthorizationInfo.addRole("护士部");
        } else if (id == 3) {
            simpleAuthorizationInfo.addRole("手术部");
        } else if (id == 4) {
            simpleAuthorizationInfo.addRole("门诊部");
        } else if (id == 5) {
            simpleAuthorizationInfo.addRole("财务部");
        } else if (id == 6) {
            simpleAuthorizationInfo.addRole("挂号部");
        }
        return simpleAuthorizationInfo;
    }

    /**
     *@描述 验证
     *@参数
     *@返回值
     *@创建人  li
     *@创建时间  2019/11/27 0027
     *@修改人和其它信息
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {

//        获取当前的角色
        String userName = (String) authenticationToken.getPrincipal();
        System.out.println("传进来的userName为===>"+userName);
        DocterInfo user = docterInfoMapper.findDoctorByAccount(userName);
        if (user==null){
            throw new UnknownAccountException("用户["+userName+"]不存在");
        }
        System.out.println("用户的密码为:===>"+user.getPassword());
        SimpleAuthenticationInfo simpleAuthenticationInfo = new SimpleAuthenticationInfo(user, user.getPassword(), getName());
        return simpleAuthenticationInfo;
    }
}
